﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;


namespace AutoChess
{

    public enum ResLoadState
    {
        None,
        Init,
        LoadScene_Ship_ing,
        LoadSceneConfig,
        LoadEntity,
        Complete,
        Waiting,
    }

    /// <summary>
    /// 匹配成功进入自走棋战斗系统
    ///     1、清除当前场景
    ///     2、请求所有玩家数据
    ///     3、加载场景、人物信息、模型信息等
    ///     4、对相关信息初始化 场景实体初始化
    ///     5、打开UI数据
    /// </summary>
     class ACResLoadState : Yunchang.FsmStateBase<AutoChessModeState>
    {
        private ResLoadState curState = ResLoadState.None;
        private AutoChessManager ctr;
       
        private float waitingTime = 1;
        private System.Action waitAction;
        private double LoadingProgress = 0;
        public ACResLoadState(AutoChessModeState stateType, AutoChessManager controller) : base(stateType, controller)
        {
            ctr = controller;
        }
        public override void Enter()
        {
            base.Enter();
            if (curState == ResLoadState.None)
                curState = ResLoadState.Init;
            //ask data
            LoadingProgress = 0.05;
            Yunchang.EventManager.TriggerEvent<string>(CommonEvent.ShowAutoChessLoading, "");
            Yunchang.EventManager.AddEventListener<AutoChessStateType>(CommonEvent.ON_STATE_TO_CHANGE_BY_SERVER, OnAllPlayerLoaded);

            AssetManager.Instance.LoadAsset(GameConst.SceneConfig);
        }


        public override void Leave()
        {
            base.Leave();
            Yunchang.EventManager.RemoveEventListener<AutoChessStateType>(CommonEvent.ON_STATE_TO_CHANGE_BY_SERVER, OnAllPlayerLoaded);
        }

        private double lastProgress = 0;
        public override void Update()
        {
            base.Update();
            if (curState == ResLoadState.Waiting)
            {
                waitingTime -= Time.deltaTime;
                if (waitingTime <= 0)
                {
                    if (waitAction != null)
                    {
                        waitAction();
                    }
                }
            }
            else if (curState == ResLoadState.Init)
            {
                AutoChessManager.Ins.SendCSAutoChessLoadingProgress(LoadingProgress, false);
                AutoChessManager.Ins.SceneCmpt.LoadCurPlayerScene();
                waitingTime = 1;
                curState = ResLoadState.Waiting;
                waitAction = () => { curState = ResLoadState.LoadScene_Ship_ing; };
            }
            else if (curState == ResLoadState.LoadScene_Ship_ing)
            {
                if (AutoChessManager.Ins.SceneCmpt.IsSceneLoadComplete)
                {
                    curState = ResLoadState.LoadSceneConfig;
                    LoadingProgress = 0.80;
                    AutoChessManager.Ins.SendCSAutoChessLoadingProgress(LoadingProgress, false);
                }
                else
                {
                    lastProgress += Time.deltaTime;
                    if ((lastProgress - 1) >= 0)//每过1s 进度涨一次
                    {
                        LoadingProgress += 0.25;
                        if (LoadingProgress >= 0.8)
                        {
                        }
                        else
                        {
                            AutoChessManager.Ins.SendCSAutoChessLoadingProgress(LoadingProgress, false);
                        }
                    }
                }
            }
            else if (curState == ResLoadState.LoadSceneConfig)
            {
                if (AssetManager.Instance.IsAssetLoaded(GameConst.SceneConfig))
                {
                    var asset = AssetManager.Instance.GetAsset(GameConst.SceneConfig);
                    AutoChessFieldViewCtr.Ins.OnLoadedSceneConfig(asset);
                    
                    waitingTime = 1;
                    curState = ResLoadState.Waiting;
                    waitAction = () => { curState = ResLoadState.LoadEntity; };
                }
            }
            else if (curState == ResLoadState.LoadEntity)
            {
                ctr.InitGameFieldUnit();
              
                waitingTime = 1;
                curState = ResLoadState.Waiting;
                waitAction = () => { curState = ResLoadState.Complete; };
            }
            else if (curState == ResLoadState.Complete)
            {
                waitingTime = 1;
                curState = ResLoadState.Waiting;
                waitAction = () => {
                    curState = ResLoadState.None;
                    AutoChessManager.Ins.SendCSAutoChessLoadingProgress(1, true);
                };
            }
        }

        /// <summary>
        /// 服务器通知可以进入了
        /// </summary>
        private void OnAllPlayerLoaded(AutoChessStateType state)
        {
            if (state == AutoChessStateType.AUTO_CHESS_STATE_ADJUST)
            {
                AutoChessManager.Ins.SceneCmpt.PlayCurSceneAudio();

                PageManager.Instance.OpenPage("AutoChessMainPage");

                AutoChessCamera.Ins.SwitchCamera(true);
                SceneUIManager.Instance.SetCamera(SceneUICameraType.ThreeD, AutoChessCamera.Ins.acCamera);

                EventManager.TriggerEvent(CommonEvent.HideLoading);
                ctr.SwitchState(AutoChessModeState.GameFieldtate);
                ctr.IsEnterSceneInit = true;
            }
        }
    }
}